package cn.manyufun.myf.common.share;

import android.app.Activity;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.social.UMPlatformData;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * @author Kevin Xiao
 * @date Create Date: 2018-02-13
 * @Description
 */
public class UmShare {

    private Activity context;
    private UMWeb mUmWeb;
    private String mTitle;

    public UmShare(Activity context, String title, String url, int drawableId) {
        this.context = context;
        UMImage umImage = new UMImage(context, drawableId);
        init(title, url, umImage);
    }

    public UmShare(Activity context, String title, String url, String imgurl) {
        this.context = context;
        UMImage umImage = new UMImage(context, imgurl);

        init(title, url, umImage);

    }

    private void init(String title, String url, UMImage image) {
        mUmWeb = new UMWeb(url);
        mUmWeb.setThumb(image);
        mUmWeb.setDescription(title);
        mUmWeb.setTitle(title);
//        mUmImage = image;
        mTitle = title;

    }

    public void Share(final UmShareShareInterface callback) {
        new ShareAction(context)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)//, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ)
                .withText(mTitle)
                .withMedia(mUmWeb)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        callback.shareCallback();
                        PostToUmeng(share_media);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        if (throwable != null) {
                            callback.onShareFailed(throwable.getMessage());
                        }

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

                    }
                }).open();
    }


    public void Share(SHARE_MEDIA media, final UmShareShareInterface callback) {
        new ShareAction(context).setPlatform(media)
                .withText(mTitle)
                .withMedia(mUmWeb)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        callback.shareCallback();
                        PostToUmeng(share_media);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        if (throwable != null) {
                            callback.onShareFailed(throwable.getMessage());
                        }

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

                    }
                }).share();
    }

    private void PostToUmeng(SHARE_MEDIA media) {
        UMPlatformData platform = new UMPlatformData(ShareMediaToUMedia(media), "0");
        MobclickAgent.onSocialEvent(context, platform);
    }

    public interface UmShareShareInterface {
        public void shareCallback();

        public void onShareFailed(String result);
    }

    private UMPlatformData.UMedia ShareMediaToUMedia(SHARE_MEDIA media) {
        switch (media) {
            case WEIXIN:
                return UMPlatformData.UMedia.WEIXIN_FRIENDS;
            case WEIXIN_CIRCLE:
                return UMPlatformData.UMedia.WEIXIN_CIRCLE;
//            case SINA:
//                return UMPlatformData.UMedia.SINA_WEIBO;
//            case QQ:;
//                return UMPlatformData.UMedia.TENCENT_QQ;
            default:
                return null;
        }
    }
}
